package com.xhwl.data.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.pojo.cent.sdata.Enterprise;
import com.xhwl.common.pojo.cent.sdata.EnterpriseExtra;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffAuditRecord;
import com.xhwl.data.dao.EnterpriseExtraDao;
import com.xhwl.data.service.IEnterpriseExtraService;
import com.xhwl.data.service.IEnterpriseService;
import com.xhwl.data.service.personal.IStaffAuditRecordService;
import com.xhwl.data.util.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 企业扩展信息表 服务实现类
 * </p>
 *
 * @author yf
 * @since 2023-02-14
 */
@Service
@Slf4j
public class EnterpriseExtraServiceImpl extends ServiceImpl<EnterpriseExtraDao, EnterpriseExtra> implements IEnterpriseExtraService {

    @Autowired
    private EnterpriseExtraDao enterpriseExtraDao;

    private static final String OPERATOR_REVIEW_ATTRIBUTE = "reviewChildrenStaff";

    private static final String ENTERPRISE_ATTRIBUTES = "enterpriseAttributes";

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private IStaffAuditRecordService staffAuditRecordService;

    @Override
    public String get(Integer enterpriseId, String attribute) {
        return enterpriseExtraDao.get(enterpriseId, "$." + attribute);
    }

    @Override
    public void save(Integer enterpriseId, JSONObject extra) {
        if (null != extra) {
            IEnterpriseExtraService enterpriseExtraService = SpringContextUtils.getBean(IEnterpriseExtraService.class);
            QueryWrapper<EnterpriseExtra> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("enterprise_id",enterpriseId);
            EnterpriseExtra enterpriseExtra = enterpriseExtraService.getOne(queryWrapper);
            if (null != enterpriseExtra){
                JSONObject jsonObject = JSONObject.parseObject(enterpriseExtra.getJson());
                jsonObject.putAll(extra);
                enterpriseExtra.setJson(JSONObject.toJSONString(jsonObject));
            }else {
                enterpriseExtra = new EnterpriseExtra();
                enterpriseExtra.setJson(JSONObject.toJSONString(extra));
            }
            enterpriseExtra.setEnterpriseId(enterpriseId);
            enterpriseExtraService.saveOrUpdate(enterpriseExtra, new QueryWrapper<EnterpriseExtra>()
                    .eq("enterprise_id", enterpriseId));
            if (extra.containsKey(OPERATOR_REVIEW_ATTRIBUTE) &&
                    "0".equals(extra.get(OPERATOR_REVIEW_ATTRIBUTE))) {
                List<Enterprise> enterprises = enterpriseService.list(new QueryWrapper<Enterprise>()
                        .eq("parent_id", enterpriseId));
                if (enterprises.size() > 0) {
                    staffAuditRecordService.update(new UpdateWrapper<StaffAuditRecord>()
                            .set("status", 1)
                            .in("enterprise_id", enterprises.stream().map(Enterprise::getId).distinct().collect(Collectors.toList()))
                            .eq("status", 2));
                }
            }
        }
    }

    @Override
    public JSONObject list(Integer enterpriseId) {
        EnterpriseExtra enterpriseExtra = enterpriseExtraDao.selectOne(new QueryWrapper<EnterpriseExtra>()
                .eq("enterprise_id", enterpriseId));
        return enterpriseExtra == null ? null : JSONObject.parseObject(enterpriseExtra.getJson(), JSONObject.class);
    }

    /**
     * 通过extra获取企业ID，南飞鸿酆泽数据同步专用，extra为 “nfh-”+"租户号"+"-R/O"
     */
    @Override
    public Integer getEnterpriseExtraByExtra(String extra) {
        EnterpriseExtra enterpriseIdByExtra = enterpriseExtraDao.getEnterpriseIdByExtra(extra);
        if (enterpriseIdByExtra != null) {
            return enterpriseIdByExtra.getEnterpriseId();
        }
        return -1;
    }

    /**
     * 为南飞鸿酆泽数据同步专门提供的接口，添加enterpriseExtra
     */
    @Override
    public void addEnterpriseExtraForNFH(Integer enterpriseId, JSONObject extra) {
        EnterpriseExtra enterpriseExtra = new EnterpriseExtra();
        enterpriseExtra.setEnterpriseId(enterpriseId);
        enterpriseExtra.setJson(JSON.toJSONString(extra));
        save(enterpriseExtra);
    }

    /**
     * 为南飞鸿酆泽数据同步专门提供的接口，通过企业ID 删除enterpriseExtra
     */
    @Override
    public void deleteEnterpriseExtraById(Integer enterpriseId) {
        getBaseMapper().delete(new QueryWrapper<EnterpriseExtra>().eq("enterprise_id", enterpriseId));
    }
}
